//
//  TopListViewController.m
//  CWRD
//
//  Created by lanou on 15/9/16.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "TopListViewController.h"
//#import "ListModel.h"
#import "DetailViewController.h"
#import "TopListTableViewCell.h"
#import "DetailModel.h"
#import "WebViewViewController.h"

@interface TopListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *weekTableView;
@property (nonatomic, strong) UITableView *monthTableView;
@property (nonatomic, strong) UITableView *totalTableView;

@property (nonatomic, strong) NSMutableArray *weekArray;
@property (nonatomic, strong) NSMutableArray *monthArray;
@property (nonatomic, strong) NSMutableArray *totalArray;

@end

@implementation TopListViewController

#pragma mark --------- 请求并解析数据 ----------
//周排行
- (void)reloadWeekListModelAndJson {
    [LORequestManger GET:kWeeklyUrl success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        for (NSDictionary *videoListDic in dic[@"videoList"]) {
            DetailModel *model = [DetailModel shareListModelWithVideoList:videoListDic consumption:videoListDic[@"consumption"]];
            [self.weekArray addObject:model];
        }
        [self.weekTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
    }];
}
- (NSMutableArray *)weekArray {
    if (!_weekArray) {
        _weekArray = [[NSMutableArray alloc] init];
    }
    return _weekArray;
}
//月排行
- (void)reloadMonthListModelAndJson {
    [LORequestManger GET:kMonthlyUrl success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        for (NSDictionary *videoListDic in dic[@"videoList"]) {
            DetailModel *model = [DetailModel shareListModelWithVideoList:videoListDic consumption:videoListDic[@"consumption"]];
            [self.monthArray addObject:model];
        }
        [self.monthTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
    }];
}
- (NSMutableArray *)monthArray {
    if (!_monthArray) {
        _monthArray = [[NSMutableArray alloc] init];
    }
    return _monthArray;
}
//总排行
- (void)reloadTotalListModelAndJson {
    [LORequestManger GET:kTotalUrl success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        for (NSDictionary *videoListDic in dic[@"videoList"]) {
            DetailModel *model = [DetailModel shareListModelWithVideoList:videoListDic consumption:videoListDic[@"consumption"]];
            [self.totalArray addObject:model];
        }
        [self.totalTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
    }];
}
- (NSMutableArray *)totalArray {
    if (!_totalArray) {
        _totalArray = [[NSMutableArray alloc] init];
    }
    return _totalArray;
}



- (void)viewDidLoad {
    [super viewDidLoad];


    self.weekTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, kWidth, kHeight - 104) style:(UITableViewStylePlain)];
    self.weekTableView.tag = 1000;
    self.weekTableView.hidden = NO;
    self.weekTableView.delegate = self;
    self.weekTableView.dataSource = self;
    [self.weekTableView registerClass:[TopListTableViewCell class] forCellReuseIdentifier:@"weekCell"];
    [self.view addSubview:self.weekTableView];
    
    self.monthTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, kWidth, kHeight - 104) style:(UITableViewStylePlain)];
    self.monthTableView.tag = 1001;
    self.monthTableView.hidden = YES;
    self.monthTableView.delegate = self;
    self.monthTableView.dataSource = self;
    [self.monthTableView registerClass:[TopListTableViewCell class] forCellReuseIdentifier:@"monthCell"];
    [self.view addSubview:self.monthTableView];
    
    self.totalTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, kWidth, kHeight - 104) style:(UITableViewStylePlain)];
    self.totalTableView.tag = 1002;
    self.totalTableView.hidden = YES;
    self.totalTableView.delegate = self;
    self.totalTableView.dataSource = self;
    [self.totalTableView registerClass:[TopListTableViewCell class] forCellReuseIdentifier:@"totalCell"];
    [self.view addSubview:self.totalTableView];
    
    [self reloadWeekListModelAndJson];
    [self reloadMonthListModelAndJson];
    [self reloadTotalListModelAndJson];
    
    [self setUpsegmentedControl];
}



#pragma mark --------- 设置segmentedControl --------

- (void)setUpsegmentedControl {
    HMSegmentedControl *segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"| 周排行 |", @"| 月排行 |", @"| 总排行 |"]];
    [segmentedControl setFrame:CGRectMake(0, 0, kWidth, 40)];

    segmentedControl.alpha = 0.9;
    segmentedControl.font = [UIFont fontWithName:@"FZLTZCHJW--GB1-0" size:14.0];
    segmentedControl.textColor = [UIColor whiteColor];
    segmentedControl.selectionIndicatorHeight = 2.0f;
    segmentedControl.selectionIndicatorColor = [UIColor cyanColor];
    segmentedControl.backgroundColor = [UIColor blackColor];
    [segmentedControl addTarget:self action:@selector(actionSegmentControl:) forControlEvents:(UIControlEventValueChanged)];
    [self.view addSubview:segmentedControl];
}

- (void)actionSegmentControl:(HMSegmentedControl *)segmentControl {
    switch (segmentControl.selectedIndex) {
        case 0:
            NSLog(@"第一个");
            self.weekTableView.hidden = NO;
            self.monthTableView.hidden = YES;
            self.totalTableView.hidden = YES;
            break;
        case 1:
            NSLog(@"第二个");
            self.weekTableView.hidden = YES;
            self.monthTableView.hidden = NO;
            self.totalTableView.hidden = YES;
            break;
        case 2:
            NSLog(@"第三个");
            self.weekTableView.hidden = YES;
            self.monthTableView.hidden = YES;
            self.totalTableView.hidden = NO;
            break;
        default:
            break;
    }
}


#pragma mark --------- tableView代理事件 --------

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (tableView.tag) {
        case 1000:
            return self.weekArray.count;
            break;
        case 1001:
            return self.monthArray.count;
            break;
        default:
            return self.totalArray.count;
            break;
    }
}
//设置cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailModel *model = [[DetailModel alloc] init];
    if (tableView.tag == 1000) {
        TopListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"weekCell" forIndexPath:indexPath];
        model = self.weekArray[indexPath.row];
        [cell.picView sd_setImageWithURL:[NSURL URLWithString:model.coverForDetail]];
        cell.rankLabel.text = [NSString stringWithFormat:@"%d.", (int)indexPath.row + 1];
        cell.titleLabel.text = model.title;
        cell.typeLabel.text = [NSString stringWithFormat:@"#%@ / %02d'%02d''", model.category, [model.duration intValue] / 60 , [model.duration intValue] % 60];
        return cell;
    }else if (tableView.tag == 1001) {
        TopListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"monthCell" forIndexPath:indexPath];
        model = self.monthArray[indexPath.row];
        [cell.picView sd_setImageWithURL:[NSURL URLWithString:model.coverForDetail]];
        cell.rankLabel.text = [NSString stringWithFormat:@"%d.", (int)indexPath.row + 1];
        cell.titleLabel.text = model.title;
        cell.typeLabel.text = [NSString stringWithFormat:@"#%@ / %02d'%02d''", model.category, [model.duration intValue] / 60 , [model.duration intValue] % 60];
        return cell;
    }else {
        TopListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"totalCell" forIndexPath:indexPath];
        model = self.totalArray[indexPath.row];
        [cell.picView sd_setImageWithURL:[NSURL URLWithString:model.coverForDetail]];
        cell.rankLabel.text = [NSString stringWithFormat:@"%d.", (int)indexPath.row + 1];
        cell.titleLabel.text = model.title;
        cell.typeLabel.text = [NSString stringWithFormat:@"#%@ / %02d'%02d''", model.category, [model.duration intValue] / 60 , [model.duration intValue] % 60];
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 150;
}
//点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    WebViewViewController *webVC = [[WebViewViewController alloc]init];
    if (tableView.tag == 1000) {
        webVC.modelArray = self.weekArray;
    }else if (tableView.tag == 1001) {
        webVC.modelArray = self.monthArray;
    }else {
        webVC.modelArray = self.totalArray;
    }
    webVC.index = indexPath.row;
    [self.navigationController pushViewController:webVC animated:YES];
}

#pragma mark --------- tableView设置表尾 --------

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 49;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footView = [[UIView alloc] init];
    footView.backgroundColor = [UIColor whiteColor];
    //设置label写the end
    UILabel *endLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, kWidth, 30)];
    endLabel.text = @"━  The End  ━";
    endLabel.textAlignment = NSTextAlignmentCenter;
    endLabel.font = [UIFont fontWithName:@"Lobster 1.4" size:18];
    [footView addSubview:endLabel];
    return footView;
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
